package com.yxhl.km.http.cookie;

import android.util.Log;

import com.yxhl.km.MyApplication;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.SharedPreferencesUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by 张亚楠 on 2018/5/18.
 * 拦截器
 */

public class HttpCookieInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        Log.e("response", response.toString());
        List<String> cookieList = response.headers("Set-Cookie");
        if (!cookieList.isEmpty()) {
            // TODO: 2016/5/3 0003 保存cookie
            Log.e("cookielist", cookieList.toString());
            String cookies = cookieList.get(0);
            SharedPreferencesUtils.setParam(MyApplication.mContext, Constants.COOKIE, cookies);
            Request.Builder builder = chain.request().newBuilder();
            builder.addHeader("Cookie", cookies);
            Log.e("cookie", cookies);
        }
        return response;
    }
}
